﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-06>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：本月托收
//********************************************************************************//
using System;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui
{
    public partial class BenYueTS : BasePage
    {
        #region 属性
        
        private string zhandianbh
        {
            get
            {
                return Convert.ToString(ViewState["zhandianbh"]);
            }
            set
            {
                ViewState["zhandianbh"] = value;
            }
        }

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 记录列表二查询结果集
        /// </summary>
        private DataTable dtSource2
        {
            get
            {
                return ViewState["dtSource2"] as DataTable;
            }
            set
            {
                ViewState["dtSource2"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 托收明细BLL
        /// </summary>
        YS_SF_TUOSHOUMXBLL _YS_SF_TUOSHOUMXBLL = new YS_SF_TUOSHOUMXBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                LoadData1(true);
                LoadData2(true);
            }
            LoadData1(false);
            LoadData2(false);
        }

        /// <summary>
        /// 管理块选择项改变事件
        /// </summary>
        protected void cmbconGuanlik_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData1(true);
            LoadData2(true);
        }

        /// <summary>
        /// 数据年月文本框回车事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnconSearch_Click(object sender, EventArgs e)
        {
            LoadData1(true);
            LoadData2(true);
        }

        /// <summary>
        /// 列表二定位
        /// </summary>
        protected void btnConGoto_Click(object sender, EventArgs e)
        {
            if (this.txtconYonghuh.Text.Trim() != "")
            {
                int id = PageTools.FocusOpreateRecord(this.dtSource2, this.gvList2, this.cmbconGotoType.SelectedIndex == 0 ? "S_YONGHUH" : "I_TUOSHOUPZH", this.txtconYonghuh.Text, true);
                if (id == -1)
                {
                    PageTools.AlertMsg(this, "当前数据中没有你要找的数据,请检查你输入的条件是否正确。");
                }
            }
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult();
            cr.OpType = e.Parameter;
            if (e.Parameter=="search")
            {                
                int id = PageTools.SearchRecordIndex(this.gvList2, this.cmbconGotoType.SelectedIndex == 0 ? "S_YONGHUH" : "I_TUOSHOUPZH", this.txtconYonghuh.Text, true);
                if (id == -1)
                {
                    cr.IsAlertMsg = true;
                    cr.Msg = "当前数据中没有你要找的数据,请检查你输入的条件是否正确。";
                }
                else
                {
                    cr.IsSuccess = true;
                    cr.AddValue(id);
                }
                e.Result = cr.ToString();
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANMC", "ZHANDIANBH", "-", string.Format("USERID={0}", LoginInfo.CurrentUser.UserID)).Tables[0];            
            this.zhandianbh = dtZhanDian.Rows[0]["VALUE"].ToString();
            //加载条件查询处的管理块
            DataTable dtConGLK = BusinessTool.GetTextAndValue(
                "YS_JX_GUANLIK", "S_GUANLIKMC", "S_GUANLIKH", null, this.zhandianbh != "00" ? string.Format("s_zhandian='{0}'", zhandianbh) : null
                ).Tables[0];
            PageTools.BindComboBox(
                this.cmbconGuanlik, dtConGLK
            );
            //加载定位选择下拉框
            this.cmbconGotoType.Items.Add("按用户号定位");
            this.cmbconGotoType.Items.Add("按托收凭证号定位");
            this.cmbconGotoType.SelectedIndex = 0;
            this.cmbconGuanlik.SelectedIndex = 0;
            //加载当前水量年月
            this.txtconYM.Text = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());   
        }

        /// <summary>
        /// 查询GRIDVIEW1数据
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadData1(bool reGet)
        {
            //加载托收进账单
            if (reGet)
            {
                this.dtSource = _spHelper.P_YSSF_GETSHOUFEIBYTYPE((int)eShouFeiJZDLX.本月托收, this.cmbconGuanlik.Value.ToString(), int.Parse(this.txtconYM.Text) / 100, int.Parse(this.txtconYM.Text) % 100, null).Tables[0];
            }
            this.gvList1.DataSource = this.dtSource;
            this.gvList1.DataBind();
            if(this.dtSource.Rows.Count>0)
            {
                this.gvList1.FocusedRowIndex = 0;
            }
        }

        /// <summary>
        /// 查询GRIDVIEW2数据
        /// </summary>
        /// <param name="reGet">是否重新读取数据库</param>
        private void LoadData2(bool reGet)
        {
            if (reGet)
            {
                int shoufeiBH = Convert.ToInt32(this.gvList1.GetRowValues(this.gvList1.FocusedRowIndex, "I_SHOUFEIBH"));
                if (shoufeiBH != -1)
                {
                    //加载进账单明细
                    this.dtSource2 = _spHelper.P_YSSF_GETTUOSHOUMX(shoufeiBH).Tables[0];                   
                }
            }
            this.gvList2.DataSource = this.dtSource2;
            this.gvList2.DataBind();
        }

        #endregion       
    }
}